package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.PageResult;
import com.junwell.common.lang.Result;
import com.junwell.entity.*;
import com.junwell.service.DriverService;
import com.junwell.service.OrdersService;
import com.junwell.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.*;
import redis.clients.jedis.params.GeoRadiusParam;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.DoubleStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/driver")
public class DriverController {

    @Autowired
    private DriverService driverService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private StationService stationService;

    @Autowired
    private JedisPool jedisPool;

    /*@Resource
    private GeoUnit unit;*/
    //@Resourse标注后在配置类中寻找对应元素
    /*@Resource
    private GeoRadiusParam georadius;*/

    @GetMapping("findPage")
    public IPage<Driver> findPage(PageDto pageDto){
        IPage<Driver> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Driver> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name"  , pageDto.getQueryString());
        page  = driverService.page(page, queryWrapper);
        return page;
    }

    //查询员工个人信息
    @GetMapping("/meInfo/{loginName}")
    public Result viewOne(@PathVariable("loginName")String loginName){
        QueryWrapper<Driver> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        Driver driver= driverService.getOne(queryWrapper);
        return Result.succ(driver);
    }

    //编辑员工的信息
    @GetMapping("/editme/{id}")
    public Result view(@PathVariable("id")Integer id){
        QueryWrapper<Driver> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Driver driver= driverService.getOne(queryWrapper);
        return Result.succ(200,"成功",driver);
    }

    //更改员工的信息
    @PostMapping("update")
    public Result update(@RequestBody Driver driver){
        boolean flag= driverService.updateById(driver);
        if (flag){
            return Result.succ("更新成功");
        }else {
            return Result.fail("更新失败");
        }
    }

    /*-----------------------------------------------------*/


    //查询订单的信息
    @GetMapping("package")
    public IPage<Orders> bag(PageDto pageDto){
        IPage<Orders> page =new Page<>(pageDto.getCurrentPage(),  pageDto.getPageSize());
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("driver_id",pageDto.getQueryString()).or().eq("driver_name",pageDto.getQueryString());
        page=ordersService.page(page,queryWrapper);
        return page;
    }

    //编辑订单的信息
    @GetMapping("/{id}")
    public Result order(@PathVariable("id")Integer id){
        boolean b = ordersService.driverSentOver(id);
        if (b){
            return Result.succ("派送完成");
        }else {
            return Result.fail("派送失败");
        }
    }

    @PostMapping("updatebag")
    public Result update(@RequestBody Orders orders){
        boolean flag=ordersService.updateById(orders);
        if (flag){
            return Result.succ("更新成功");
        }else {
            return Result.fail("更新失败");
        }
    }



    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id ){
        boolean delete = ordersService.delete(id);
        if (delete){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }



        /*--------------------------------------------------*/


    //根据员工的定位
    @GetMapping("station")
    public IPage<Station> address(PageDto pageDto){
        IPage<Station> page=new Page<>();
        QueryWrapper<Station > queryWrapper=new QueryWrapper<>();
        queryWrapper.like("station_name",pageDto.getQueryString());
        page=stationService.page(page,queryWrapper);
        return page;
    }

    //根据员工的位置定位，搜索附近的站点位置
    @GetMapping("car")
    public IPage<Station> car(PageDto pageDto,Double longitude,Double latitude,String key){
        IPage<Station> page=new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Station > queryWrapper=new QueryWrapper<>();
        queryWrapper.like("station_name","");
        String queryString = pageDto.getQueryString();
        double radius=Double.parseDouble(queryString);
        page=stationService.page(page,queryWrapper);
        Jedis jedis = jedisPool.getResource();
        System.out.println(jedis.zrem("station", key));
        System.out.println(jedis.geoadd("station", longitude, latitude, key));
        /*jedis.geoadd("china:city",jd,wd,"car");*/
        System.out.println("员工地址"+jedis.geopos("station", key));
        List<GeoCoordinate> car = jedis.geopos("station", key);
        double l1 = car.get(0).getLatitude();
        System.out.println(l1);
        double l2 = car.get(0).getLongitude();
        System.out.println(l2);
        List<GeoRadiusResponse> list = jedis.georadius("station", longitude, latitude,radius , GeoUnit.KM,GeoRadiusParam.geoRadiusParam().withDist());
        for (int i=0;i<page.getRecords().size();i++){
            for (int j = 0; j<list.size(); j++) {
                if (page.getRecords().get(i).getStationName().equals(list.get(j).getMemberByString())) {
                    page.getRecords().get(i).setDistance(list.get(j).getDistance());
                }
            }
        }
        jedis.close();
        return page;

    }

    //查找目的地（站点）的位置经纬度
    @GetMapping("findWay/{city}")
    public List<GeoCoordinate> findWay(@PathVariable ("city")String city ){
        Jedis jedis = jedisPool.getResource();
        List<GeoCoordinate> list = jedis.geopos("station", city);
        jedis.close();
        return list;
    }

    //查找途径点
    @GetMapping("waypoint")
    public List<GeoCoordinate> waypoint(){
        Jedis jedis = jedisPool.getResource();
        List<GeoCoordinate> list = jedis.geopos("station", "常州钟楼站", "常州站", "常州郑陆站", "无锡新吴站", "苏州姑苏站", "苏州站", "无锡站", "南京秦淮站", "南京总站", "镇江丹徒站", "镇江站");
        jedis.close();
        return list;
    }



}
